package com.wss.controller;

import com.alibaba.fastjson.JSON;
import com.wss.domain.Message;
import com.wss.domain.Order;
import com.wss.domain.Video;
import com.wss.service.OrderService;
import com.wss.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private VideoService videoService;

    /**
     * 订单详情页
     */
    @RequestMapping(value="/orderPage")
    public ModelAndView orderPage(int u_id, int v_id, Model model){
        Video video = videoService.findVideoById(v_id);
        model.addAttribute("video",video);
        model.addAttribute("u_id",u_id);
        return new ModelAndView("orderPage");
    }

    /**
     * 添加订单
     */
    @RequestMapping(value="/addOrder", method= RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String addOrder(Order order){
        Message message = orderService.addOrder(order);
        String msg = JSON.toJSONString(message);
        return msg;
    }

    /**
     * 删除订单
     */
    @RequestMapping(value="/deleteOrder", method= RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String deleteOrder(Order order){
        Message message = orderService.deleteOrder(order);
        String msg = JSON.toJSONString(message);
        return msg;
    }

}
